//
//  LSNavigationViewController.m
//  LookScore
//
//  Created by wr on 2017/12/4.
//  Copyright © 2017年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSNavigationViewController.h"
#import "LSHomeVController.h"
#import "LSMineView.h"
#import "LSMineVController.h"
#import "LSLoginVController.h"
#import "LSExponentVController.h"
@interface LSNavigationViewController ()<UINavigationControllerDelegate>

@end

@implementation LSNavigationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.enabled =NO;
    self.interactivePopGestureRecognizer.delegate = nil;
//    self.navigationBar.barTintColor = LSNavMainColor;
//    self.navigationBar.translucent = NO;
//    self.navigationBar.tintColor = LSTitleWhiteColor;
//    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:LSNavTitlenColor}];
//
//    [self.navigationBar setBackgroundImage:[CommonClass getImageWithBgColor:LSNavMainColor] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationBar setShadowImage:[[UIImage alloc]init]];
//
    //设置整个项目中所有item的主题样式
    UIBarButtonItem *item = [UIBarButtonItem appearance];

    //普通状态
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];

    //不可用状态
    NSMutableDictionary *disabletextAttrs = [NSMutableDictionary dictionary];
    disabletextAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    disabletextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:disabletextAttrs forState:UIControlStateDisabled];


    //左
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
    headerButton.layer.cornerRadius = headerButton.bounds.size.width/2.0f;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"NavFiltrate"] forState:UIControlStateNormal];
    [headerButton addTarget:self action:@selector(CenterEvent) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];

    UIButton *NavSettingBu = [[UIButton alloc]initWithFrame:CGRectMake(0,0,22,22)];
    [NavSettingBu setImage:[UIImage imageNamed:@"NavSetting"]forState:UIControlStateNormal];
    [NavSettingBu addTarget:self action:@selector(RightEvent:) forControlEvents:UIControlEventTouchUpInside];
    NavSettingBu.tag=10;
    UIBarButtonItem *NavSettingBui = [[UIBarButtonItem alloc]initWithCustomView:NavSettingBu];

    UIButton *DataBankBu = [[UIButton alloc]initWithFrame:CGRectMake(0,0,22,22)];
    [DataBankBu setImage:[UIImage imageNamed:@"NavDataBank"]forState:UIControlStateNormal];
    [DataBankBu addTarget:self action:@selector(RightEvent:) forControlEvents:UIControlEventTouchUpInside];
    DataBankBu.tag=11;
    UIBarButtonItem *DataBankBuI = [[UIBarButtonItem alloc]initWithCustomView:DataBankBu];
    self.navigationItem.rightBarButtonItems=@[NavSettingBui ,DataBankBuI];
    
    
    
   
}

/**
 *  自定义NavigationController 重写 pushViewController
 *  使所有 NavigationController 的左上角和右上角功能和图标一致
 *  @param viewController viewController
 *  @param animated       animated
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (self.childViewControllers.count) { // 隐藏导航栏
        viewController.hidesBottomBarWhenPushed = YES;
        viewController.navigationController.navigationBar.hidden=NO;
        
//        UIButton * btn  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
//        [btn setImage:[UIImage imageNamed:@"ReturnCustom"] forState:UIControlStateNormal];
//        
//        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//        [btn addTarget:self action:@selector(returnCustom) forControlEvents:UIControlEventTouchUpInside];
//         自定义返回按钮
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed: @"ReturnCustom"] style:UIBarButtonItemStylePlain target:self action:@selector(returnCustom)];
        
        
        
//        // 如果自定义返回按钮后, 滑动返回可能失效, 需要添加下面的代码

        __weak typeof(viewController)Weakself = viewController;
        self.interactivePopGestureRecognizer.enabled = YES;
        self.interactivePopGestureRecognizer.delegate = (id)Weakself;


    }
    [super pushViewController:viewController animated:animated];
   
       
}

-(void)dismissViewController{
    
    if ((self.navigationController.presentedViewController || self.navigationController.presentingViewController) && self.navigationController.childViewControllers.count == 1) {
        
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)returnCustom{
    //判断两种情况: push 和 present
    if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
       
        
        [self popViewControllerAnimated:YES];
    }
    
    
}


- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item {
    //只有一个控制器的时候禁止手势，防止卡死现象
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    if (self.childViewControllers.count > 1) {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = YES;
        }
    }
    return YES;
}
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item {
    //只有一个控制器的时候禁止手势，防止卡死现象
    if (self.childViewControllers.count == 1) {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end



@implementation UIViewController (LSPresent_Navigation)


-(void)presentRootViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{
    LSNavigationViewController *nav=[[LSNavigationViewController alloc]initWithRootViewController:viewControllerToPresent];
    UIButton *backItem=[UIButton buttonWithType:UIButtonTypeCustom];
    
    backItem.frame=CGRectMake(0,0,30, 30);
    [backItem setImage:[UIImage imageNamed:@"ReturnCustom"] forState:UIControlStateNormal];
    [backItem addTarget:viewControllerToPresent action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
    [backItem setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 10)];
    UIBarButtonItem *barItemBtn=[[UIBarButtonItem alloc]initWithCustomView:backItem];
    viewControllerToPresent.navigationItem.leftBarButtonItem=barItemBtn;
    
//    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:viewControllerToPresent];
    
    nav.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
//    nav.modalPresentationStyle=UIModalPresentationCustom;
    
    [self presentViewController:nav animated:flag completion:completion];

//    [self presentRootViewController:nav animated:flag completion:completion];



    
}



-(void)dismissViewController{
    
    if ((self.navigationController.presentedViewController || self.navigationController.presentingViewController) && self.navigationController.childViewControllers.count == 1) {
//        [notifiCenter postNotificationName:@"ChatKeyboardShouldResignFirstResponder" object:nil];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChatKeyboardShouldResignFirstResponder" object:@(1)];
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}



-(void)dismissRootViewControllerLevel:(int)level{
    
    __block int index=level;
    
    if (level!=0) {
        
        [[CommonClass getTopCurrentViewController]dismissViewControllerAnimated:YES completion:^{
            
            index=index-1;
            
            if (index!=0) {
                
                [self dismissRootViewControllerLevel:index];
            }
        }];
    }
}

@end
